
//
//  MCLogisticsCompanyViewController.swift
//  StoreManage
//
//  Created by MC on 2017/12/21.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

protocol MCLogisticsCompanyViewControllerDelegate : NSObjectProtocol {
    func MCLogisticsCompanyViewController_selectedCompany(model:MCLogisticsCompanyModel)
}

class MCLogisticsCompanyViewController: MCViewController {
    
    weak var delegate : MCLogisticsCompanyViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    
    // MARK: - Network
    func sendNetworking() {
        let para = [
            "data" : 1
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_listBaExpressCompany, para: para as [String : AnyObject], vc: self,hudSuperView: self.view,isHiddenTabbar: true, success: { (response) in
        
            let dataArray = response["data"] as! NSArray
            
            for dict in dataArray {
                let model = MCLogisticsCompanyModel()
                model.setValuesForKeys(dict as! [String : Any])
                self.dataArrayM.add(model)
            }
            self.tableView.reloadData()
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "物流公司"
        self.view.backgroundColor = UIColor.white
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
}

extension MCLogisticsCompanyViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let model = self.dataArrayM[indexPath.row] as! MCLogisticsCompanyModel
        
        
        let cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        cell.textLabel?.text = model.companyName
        
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        cell.contentView.addSubview(view)
        view.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataArrayM[indexPath.row] as! MCLogisticsCompanyModel
        delegate?.MCLogisticsCompanyViewController_selectedCompany(model: model)
        self.navigationController?.popViewController(animated: true)
    }
}

